<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\pay\model;

use think\facade\Db;
use think\Model;
use tree\Tree;

class MemberModel extends Model
{

    /**
     * 选择用户
     * @param $currentIds
     * @param $tpl
     * @param $where
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userTableTree($currentIds = 0, $tpl = '', $where = [])
    {
        $categories = Db::name("user")
            ->field("id,pid as parent_id,user_login")
            ->where($where)
            ->select()->toArray();
        $tree = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';

        if (!is_array($currentIds)) {
            $currentIds = [$currentIds];
        }
        $newCategories = [];
        foreach ($categories as $item) {
            if (cmf_auth_check(cmf_get_current_admin_id(), 'pay/member/index?id=' . $item['id'])) {
                $item['parent_id'] = 0;
                $item['category_name'] = $item['user_login'];
                $item['parent_id_node'] = ($item['parent_id']) ? ' class="child-of-node-' . $item['parent_id'] . '"' : '';
                $item['style'] = empty($item['parent_id']) ? '' : 'display:none;';
                $item['checked'] = in_array($item['id'], $currentIds) ? "checked" : "";
                array_push($newCategories, $item);
            }
        }
        $tree->init($newCategories);
        if (empty($tpl)) {
            $tpl = "<tr id='node-\$id' \$parent_id_node style='\$style' data-parent_id='0' data-id='\$id'>
                        <td style='padding-left:20px;'><input type='checkbox' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids[]' value='\$id' data-parent_id='\$parent_id' data-id='\$id'></td>
                        <td>\$id</td>
                        <td>\$user_login</td>
                    </tr>";
        }
        $treeStr = $tree->getTree(0, $tpl);
        return $treeStr;
    }


}